/*
 */

package com.railway.news.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.org.rapid_framework.page.Page;
import com.railway.common.base.BaseManager;
import com.railway.common.base.EntityDao;
import com.railway.common.base.IdGenerator;
import com.railway.common.constants.SysConfigFunc;
import com.railway.common.util.PageRequestFactory;
import com.railway.news.dao.ICategoryDao;
import com.railway.news.model.Category;
import com.railway.news.model.CategoryUserLink;
import com.railway.news.service.ICategoryManager;
import com.railway.news.vo.query.CategoryQuery;
import com.railway.system.dao.ISysFrelDao;
import com.railway.system.model.SysFrel;
import com.railway.system.vo.query.SysFrelQuery;

/**
 * @author jiangcheng
 * @version 1.0
 * @since 1.0
 */

@Service("categoryManager")
@Transactional
public class CategoryManagerImpl extends
		BaseManager<Category, java.lang.String> implements ICategoryManager {
	private ISysFrelDao sysFrelDao;
	private ICategoryDao categoryDao;

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写 */
	public void setCategoryDao(ICategoryDao dao) {
		this.categoryDao = dao;
	}

	public EntityDao getEntityDao() {
		return (EntityDao) this.categoryDao;
	}

	@Transactional(readOnly = true)
	public Page findPage(CategoryQuery query) {
		return categoryDao.findPage(query);
	}

	@Transactional(readOnly = true)
	public Category getById(java.lang.String id) {
		return super.getById(id);
	}

	@Transactional
	public void removeById(java.lang.String id) {
		/**
		 * 先删除关联表里的相应栏目相关的信息
		 */
		this.categoryDao.deleteCategoryUserLinkByCatId(id);
		super.removeById(id);
	}

	/** 插入数据 */
	@Transactional
	public void save(Category entity) {
		// 获得审核人id列表,并批量插入 T_SYS_CATEGORY_USERS_LINK
		String checkorids = entity.getCheckerid();
		/**
		 * 维护人id
		 */
		String maintainerids = entity.getMaintainerid();
		// 首先，保存新闻分类的实体信息
		if ("VIDEO".equals(entity.getCaTypes())) {
			
			//保存操作权限
			if (maintainerids != null) {
				String[] uids = maintainerids.split(",");
				for (int i = 0; i < uids.length; i++) {
					
					SysFrelQuery query = (SysFrelQuery) PageRequestFactory
							.bindPageRequest(
									org.springframework.beans.BeanUtils
											.instantiateClass(SysFrelQuery.class),
									ServletActionContext.getRequest(), null);
					query.setFuncId(SysConfigFunc.func_VIDEO);
					query.setRoleId(uids[i]);
					List<SysFrel> listSysFrel = sysFrelDao.find(query);
					if (listSysFrel == null || listSysFrel.size() == 0) {
						SysFrel sysFrel = new SysFrel();
						sysFrel.setFuncId(SysConfigFunc.func_VIDEO);
						sysFrel.setRoleId(uids[i]);
						sysFrelDao.save(sysFrel);
					}
				}
			}
		} else if ("PHOTO".equals(entity.getCaTypes())) {
			//保存操作权限
			if (maintainerids != null) {
				String[] uids = maintainerids.split(",");
				for (int i = 0; i < uids.length; i++) {
					
					SysFrelQuery query = (SysFrelQuery) PageRequestFactory
							.bindPageRequest(
									org.springframework.beans.BeanUtils
											.instantiateClass(SysFrelQuery.class),
									ServletActionContext.getRequest(), null);
					query.setFuncId(SysConfigFunc.func_PHOTO);
					query.setRoleId(uids[i]);
					List<SysFrel> listSysFrel = sysFrelDao.find(query);
					if (listSysFrel == null || listSysFrel.size() == 0) {
						SysFrel sysFrel = new SysFrel();
						sysFrel.setFuncId(SysConfigFunc.func_PHOTO);
						sysFrel.setRoleId(uids[i]);
						sysFrelDao.save(sysFrel);
					}
				}
			}
		} else {
			super.save(entity);
		}
		List list = new ArrayList();

		if (checkorids != null) {
			String[] uids = checkorids.split(",");
			for (int i = 0; i < uids.length; i++) {
				if ("".equals(uids[i])) {
					continue;
				}
				CategoryUserLink culink = new CategoryUserLink();
				culink.setId(IdGenerator.genUUIDStr());
				culink.setCategoryid(entity.getId());
				culink.setState("C");
				culink.setUserid(uids[i]);
				list.add(culink);

				SysFrelQuery query = (SysFrelQuery) PageRequestFactory
						.bindPageRequest(org.springframework.beans.BeanUtils
								.instantiateClass(SysFrelQuery.class),
								ServletActionContext.getRequest(), null);
				query.setFuncId(SysConfigFunc.func_check);
				query.setRoleId(uids[i]);
				List<SysFrel> listSysFrel = sysFrelDao.find(query);
				if (listSysFrel == null || listSysFrel.size() == 0) {
					SysFrel sysFrel = new SysFrel();
					sysFrel.setFuncId(SysConfigFunc.func_check);
					sysFrel.setRoleId(uids[i]);
					sysFrelDao.save(sysFrel);
				}

			}

		}
		
		if (maintainerids != null) {

			String[] uids = maintainerids.split(",");
			for (int i = 0; i < uids.length; i++) {
				if ("".equals(uids[i])) {
					continue;
				}
				CategoryUserLink culink = new CategoryUserLink();
				culink.setId(IdGenerator.genUUIDStr());
				culink.setCategoryid(entity.getId());
				culink.setState("M");
				culink.setUserid(uids[i]);
				list.add(culink);

				SysFrelQuery query = (SysFrelQuery) PageRequestFactory
						.bindPageRequest(org.springframework.beans.BeanUtils
								.instantiateClass(SysFrelQuery.class),
								ServletActionContext.getRequest(), null);
				query.setFuncId(SysConfigFunc.returnFunId(entity
						.getPublicType()));
				query.setRoleId(uids[i]);
				List<SysFrel> listSysFrel = sysFrelDao.find(query);
				if (listSysFrel == null || listSysFrel.size() == 0) {
					SysFrel sysFrel = new SysFrel();
					sysFrel.setFuncId(SysConfigFunc.returnFunId(entity
							.getPublicType()));
					sysFrel.setRoleId(uids[i]);
					sysFrelDao.save(sysFrel);
				}
			}

		}
		/**
		 * 访问者id
		 */
		String visitorids = entity.getVisitorid();
		if (visitorids != null) {

			String[] uids = visitorids.split(",");
			for (int i = 0; i < uids.length; i++) {
				if ("".equals(uids[i])) {
					continue;
				}
				CategoryUserLink culink = new CategoryUserLink();
				culink.setId(IdGenerator.genUUIDStr());
				culink.setCategoryid(entity.getId());
				culink.setState("V");
				culink.setUserid(uids[i]);
				list.add(culink);
			}

		}
		this.categoryDao.deleteCategoryUserLinkByCatId(entity.getId());
		this.categoryDao.saveCategoryUserLinkBatch(list);
	}

	/** 更新数据 */
	@Transactional
	public void update(Category entity) {

		super.update(entity);
		/**
		 * 先删除关联表里的相应栏目相关的信息
		 */
		this.categoryDao.deleteCategoryUserLinkByCatId(entity.getId());
		// 重新建立
		List list = new ArrayList();
		// 获得审核人id列表,并批量插入 T_SYS_CATEGORY_USERS_LINK
		String checkorids = entity.getCheckerid();
		if (checkorids != null) {

			String[] uids = checkorids.split(",");
			for (int i = 0; i < uids.length; i++) {
				if ("".equals(uids[i])) {
					continue;
				}
				CategoryUserLink culink = new CategoryUserLink();
				culink.setId(IdGenerator.genUUIDStr());
				culink.setCategoryid(entity.getId());
				culink.setState("C");
				culink.setUserid(uids[i]);
				list.add(culink);

				SysFrelQuery query = (SysFrelQuery) PageRequestFactory
						.bindPageRequest(org.springframework.beans.BeanUtils
								.instantiateClass(SysFrelQuery.class),
								ServletActionContext.getRequest(), null);
				query.setFuncId(SysConfigFunc.func_check);
				query.setRoleId(uids[i]);
				List<SysFrel> listSysFrel = sysFrelDao.find(query);
				if (listSysFrel == null || listSysFrel.size() == 0) {
					SysFrel sysFrel = new SysFrel();
					sysFrel.setFuncId(SysConfigFunc.func_check);
					sysFrel.setRoleId(uids[i]);
					sysFrelDao.save(sysFrel);
				}
			}

		}
		/**
		 * 维护人id
		 */
		String maintainerids = entity.getMaintainerid();
		if (maintainerids != null) {

			String[] uids = maintainerids.split(",");
			for (int i = 0; i < uids.length; i++) {
				if ("".equals(uids[i])) {
					continue;
				}
				CategoryUserLink culink = new CategoryUserLink();
				culink.setId(IdGenerator.genUUIDStr());
				culink.setCategoryid(entity.getId());
				culink.setState("M");
				culink.setUserid(uids[i]);
				list.add(culink);

				SysFrelQuery query = (SysFrelQuery) PageRequestFactory
						.bindPageRequest(org.springframework.beans.BeanUtils
								.instantiateClass(SysFrelQuery.class),
								ServletActionContext.getRequest(), null);
				query.setFuncId(SysConfigFunc.returnFunId(entity
						.getPublicType()));
				query.setRoleId(uids[i]);
				List<SysFrel> listSysFrel = sysFrelDao.find(query);
				if (listSysFrel == null || listSysFrel.size() == 0) {
					SysFrel sysFrel = new SysFrel();
					sysFrel.setFuncId(SysConfigFunc.returnFunId(entity
							.getPublicType()));
					sysFrel.setRoleId(uids[i]);
					sysFrelDao.save(sysFrel);
				}

			}

		}
		/**
		 * 访问者id
		 */
		String visitorids = entity.getVisitorid();
		if (visitorids != null) {

			String[] uids = visitorids.split(",");
			for (int i = 0; i < uids.length; i++) {
				if ("".equals(uids[i])) {
					continue;
				}
				CategoryUserLink culink = new CategoryUserLink();
				culink.setId(IdGenerator.genUUIDStr());
				culink.setCategoryid(entity.getId());
				culink.setState("V");
				culink.setUserid(uids[i]);
				list.add(culink);
			}

		}
		this.categoryDao.deleteCategoryUserLinkByCatId(entity.getId());
		this.categoryDao.saveCategoryUserLinkBatch(list);

	}

	@Transactional(readOnly = true)
	public boolean isUnique(Category entity, String uniquePropertyNames) {
		return super.isUnique(entity, uniquePropertyNames);
	}

	@Transactional(readOnly = true)
	public List<Category> findAll() {
		//
		return categoryDao.findAll();
	}

	@Transactional(readOnly = true)
	public List<Category> findByType(String type) {
		return categoryDao.findByType(type);
	}

	@Transactional(readOnly = true)
	public List getPermitCategoriesByUser(CategoryUserLink culinkquery) {
		// 首先,提取该分类可以操作的
		return this.categoryDao.getPermitCategoriesByUser(culinkquery);
	}

	@Transactional
	public void saveOrUpdate(Category entity) {
		super.saveOrUpdate(entity);
	}
	@Transactional
	public void deleteCategoryUserLinkById(String id){
		categoryDao.deleteCategoryUserLinkById(id);
	}
	@Transactional
	public void removeLinkByUserId(String userId) {
		// TODO Auto-generated method stub
		categoryDao.removeLinkByUserId(userId);
	}

	@Transactional
	public void saveCategoryUserLink(CategoryUserLink entity) {
		categoryDao.saveCategoryUserLink(entity);
	}

	@Transactional
	public void saveCategoryUserLink(List list) {

		this.categoryDao.saveCategoryUserLinkBatch(list);

	}

	@Transactional(readOnly = true)
	public List getUsernamesByMap(Map map) {
		// TODO Auto-generated method stub
		return this.categoryDao.getUsernamesByMap(map);
	}

	public void setSysFrelDao(ISysFrelDao sysFrelDao) {
		this.sysFrelDao = sysFrelDao;
	}
}
